package game.breeze.socket.connection;

import game.breeze.socket.util.BufferWrapper;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

/**
 * MinaConnection 使用MINA框架实现的网络连接类
 */
public class MinaConnection extends NetConnection {
	private IoSession ioSession;
	private String ip;

	public MinaConnection(IoSession session, BufferWrapper buffer) {
		super(buffer);
		ioSession = session;
		ip = session.getRemoteAddress().toString();
	}

	protected void sendDataImpl(byte[] data, int offset, int count) {
		IoBuffer buffer = IoBuffer.allocate(count);
		buffer.put(data, offset, count);
		buffer.flip();
		if (ioSession != null && !ioSession.isClosing()) {
			ioSession.write(buffer);
		}
	}

	public void close() {
		if (ioSession != null)
			ioSession.close(true);

		ioSession = null;
	}

	public String getIP() {
		return ip;
	}

	public boolean isActive() {
		return ioSession != null;
	}
}
